package com.springboot.erp.service.impl;

import com.springboot.erp.entity.ExpenseDTO;
import com.springboot.erp.entity.JshAccountHead;
import com.springboot.erp.mapper.ExpenseMapper;
import com.springboot.erp.service.ExpenseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j


public class ExpenseServiceImpl implements ExpenseService {

    @Resource
    private ExpenseMapper ExpenseMapper;

    @Resource
    private ExpenseMapper expenseMapper;

    @Override
    public ExpenseDTO selectExpenseById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("查询支出单时，ID不能为空");
        }
        return expenseMapper.selectExpenseById(id);
    }

    @Override
    public List<JshAccountHead> listExpenses(Long organId, String status, Date startDate, Date endDate) {
        if (startDate != null && endDate != null && startDate.after(endDate)) {
            throw new IllegalArgumentException("查询支出单列表时，开始日期不能晚于结束日期");
        }
        return expenseMapper.selectExpenseList(organId, status, startDate, endDate);
    }

    @Override
    public void addOne(JshAccountHead jshAccountHead) {
        if (jshAccountHead.getBillNo() == null || jshAccountHead.getTotalPrice() == null) {
            throw new IllegalArgumentException("添加支出单时，单据编号和总金额为必填项");
        }
        expenseMapper.insertOne(jshAccountHead);
    }

    @Override
    public void updateOne(JshAccountHead jshAccountHead) {
        if (jshAccountHead.getId() == null) {
            throw new IllegalArgumentException("更新支出单时，ID不能为空");
        }
        ExpenseDTO expense = selectExpenseById(Long.valueOf(jshAccountHead.getId()));
        if (expense == null) {
            log.warn("更新失败：ID为{}的支出单不存在", jshAccountHead.getId());
            return;
        }
        expenseMapper.updateOne(jshAccountHead);
    }

    @Override
    public void deleteOne(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("删除支出单时，ID不能为空");
        }
        ExpenseDTO expense = selectExpenseById(id.longValue());
        if (expense == null) {
            log.warn("删除失败：ID为{}的支出单不存在", id);
            return;
        }
        expenseMapper.deleteOne(id);
    }
}
